var vm_jingfeipicimanage = new Vue({
    el : "#jingfeipicimanage",
    data : {
        deptid : $("#deptid").val(),		// 单位ID

        jingfeipicilist : [],// 专家变动列表custom
        jingfeipiciid : '',//变动id
        jingfeipici : {},//变动表

        //查询用字段
        picimingcheng : '',

        //
        editflag : '',

        deptlist:[], //发放单位list
        deptids:'', //勾选发放单位ids
        zhuanjialist:[], //发放专家list
        zhuanjiaids:'' //勾选发放专家ids

    },
    created : function(){
        var _this = this;

        _this.bindJingfeiPiciList();

    },
    methods : {

        // 根据单位ID查询下级单位
        bindJingfeiPiciList : function(){
            var _this = this;

            var jingfeipici_layer = layer.open({type:3});
            $.post('/system/jingfeipici/find_custom_list', {
                deptid : _this.deptid,
                picimingcheng : _this.picimingcheng,
                rdm : Math.random()
            },function(ppData) {
                layer.close(jingfeipici_layer);

                if (ppData != null) {
                    if (ppData.result==1){
                        var data = ppData.resultContent;
                        _this.jingfeipicilist = data;
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },

        // 发放查询单位列表
        bindDeptList : function(jingfeipiciId){
            var _this = this;

            $.post('/system/jingfeidept/fafanglist', {
                deptId : _this.deptid,
                jingfeipiciId:jingfeipiciId,
                rdm : Math.random()
            },function(ppData) {
                if (ppData != null) {
                    if (ppData.result==1){
                        var data = ppData.resultContent;
                        _this.deptlist = data.deptList;
                        _this.zhuanjialist = data.zhuanjiaList;
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },



        getKaishinianyue:function(){
            var _this = this;

            _this.jingfeipici.kaishinianyue=$("#kaishinianyue").val();
        },

        getJieshunianyue:function(){
            var _this = this;

            _this.jingfeipici.jieshunianyue=$("#jieshunianyue").val();
        },

        getJiezhishijian:function(){
            var _this = this;

            _this.jingfeipici.jisuanshijian=$("#jisuanshijian").val();
        },

        toAdd:function(){

            $("#myModalLabel").html("添加经费批次");
            $("#modal-dialog").attr("style","width:1200px;");
            $("#jingfeipiciModal").modal();

            this.jingfeipici = {};
            this.editflag = 1;
        },

        //
        add : function(){
            var _this = this;

            if (_this.checkInputData()) {
                var jingfeipici_layer = layer.open({type:3});
                $.post('/system/jingfeipici/addcustom', {
                    deptid : _this.deptid,
                    jingfeipicimingcheng : _this.jingfeipici.jingfeipicimingcheng,
                    kaishinianyue : _this.jingfeipici.kaishinianyue,
                    jieshunianyue : _this.jingfeipici.jieshunianyue,
                    jisuanshijian : _this.jingfeipici.jisuanshijian,
                    beizhu1 : _this.jingfeipici.beizhu1,
                    rdm : Math.random()
                },function(ppData) {
                    layer.close(jingfeipici_layer);

                    if (ppData != null) {
                        if (ppData.code == "0"){

                            layer.open({
                                time: 1000,
                                btn: [],
                                content: "添加成功!",
                            });

                            _this.bindJingfeiPiciList();
                            $("#jingfeipiciModal").modal('hide');
                        }else{
                            layer.alert(ppData.msg);
                        }
                    }
                }, "json");
            }
        },

        toModify:function(ppID){

            $("#myModalLabel").html("修改经费批次");
            $("#modal-dialog").attr("style","width:1200px;");
            $("#jingfeipiciModal").modal();

            this.jingfeipiciid = ppID;

            this.jingfeipici = {};
            this.editflag = 2;
            // 绑定变动
            this.bindJingfeipici();
        },

        // 修改信息
        modify : function(){
            var _this = this;
            if (_this.checkInputData()) {
                var jingfeipici_layer = layer.open({type: 3});
                $.post('/system/jingfeipici/editcustom', {
                    id: _this.jingfeipiciid,
                    jingfeipicimingcheng : _this.jingfeipici.jingfeipicimingcheng,
                    kaishinianyue : _this.jingfeipici.kaishinianyue,
                    jieshunianyue : _this.jingfeipici.jieshunianyue,
                    jisuanshijian : _this.jingfeipici.jisuanshijian,
                    beizhu1 : _this.jingfeipici.beizhu1,
                    rdm: Math.random()
                }, function (ppData) {
                    layer.close(jingfeipici_layer);
                    if (ppData != null) {

                        if (ppData.code == "0") {

                            layer.open({
                                time: 1000,
                                btn: [],
                                content: "修改成功!",
                            });

                            _this.bindJingfeiPiciList();
                            $("#jingfeipiciModal").modal('hide');

                        } else {
                            layer.alert(ppData.msg);
                        }
                    }
                }, "json");
            }
        },

        bindJingfeipici:function(){
            var _this = this;

            var jingfeipici_layer = layer.open({type:3});
            $.post('/system/jingfeipici/findone', {
                id : _this.jingfeipiciid,
                rdm : Math.random()
            },function(ppData) {
                layer.close(jingfeipici_layer);

                if(ppData != null){

                    if(ppData.result == "1"){
                        _this.jingfeipici = ppData.resultContent;

                        if (_this.jingfeipici.beizhu1==='1'){
                            $("#shifoudangqianpici").attr("checked","true")
                        }else{
                            $("#shifoudangqianpici").attr("checked","false")
                        }

                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },

        Delete:function(jingfeipiciitem){
            var _this = this;

            $.modal.confirm("是否确认删除:"+jingfeipiciitem.jingfeipicimingcheng,function(){
                $.post('/system/jingfeipici/remove', {
                    ids : jingfeipiciitem.id,
                    rdm : Math.random()
                },function(ppData) {

                    if(ppData != null){

                        if(ppData.code == "0"){
                            layer.open({
                                time: 1000,
                                btn: [],
                                content: "删除成功!",
                            });
                            _this.bindJingfeiPiciList();
                        }else{
                            layer.alert(ppData.msg);
                        }
                    }
                },"json");
            });
        },

        Export:function(jingfeipiciitem){
            var _this = this;
            location.href = "/export/daochu_bokuanbiao?piciid="+jingfeipiciitem.id
        },


        //检查项目数据
        checkInputData : function() {
            if (!$.trim(this.jingfeipici.jingfeipicimingcheng)) {
                layer.alert("请填写经费批次名称！");
                return false;
            }

            if (!$.trim(this.jingfeipici.kaishinianyue)) {
                layer.alert("请选择开始年月！");
                return false;
            }

            if (!$.trim(this.jingfeipici.jieshunianyue)) {
                layer.alert("请选择结束年月！");
                return false;
            }

            if (!$.trim(this.jingfeipici.jisuanshijian)) {
                layer.alert("请选择截止时间！");
                return false;
            }

            this.jingfeipici.beizhu1 = $("#shifoudangqianpici").is(":checked") ? "1" : "0";

            return true;
        },

        toJisuan : function(jingfeipiciitem){
            var _this = this;
                var jingfeipici_layer = layer.open({type: 3});
                $.post('/system/jingfeipici/jisuan', {
                    id: jingfeipiciitem.id,
                    deptid : _this.deptid,
                    zhuanjiazhongxinpiciid : jingfeipiciitem.zhuanjiazhongxinpiciid,
                    jisuanshijian : jingfeipiciitem.jisuanshijian,
                    rdm: Math.random()
                }, function (ppData) {
                    layer.close(jingfeipici_layer);
                    if (ppData != null) {

                        if (ppData.result == "1") {

                            layer.open({
                                time: 1000,
                                btn: [],
                                content: "计算成功!",
                            });

                            _this.bindJingfeiPiciList();

                        } else {
                            layer.alert(ppData.message);
                        }
                    }
                }, "json");
        },

        //发放经费
        toFafangJingfei: function (jingfeipiciitem) {
            var _this = this
            _this.jingfeipiciid = jingfeipiciitem.id
            _this.bindDeptList(jingfeipiciitem.id)
            $("#fafangLabel").html("发放");
            $("#fafangmodal-dialog").attr("style","width:1500px;");
            $("#fafangModal").modal();
        },

        //发放经费
        FafangJingfei: function () {
            var _this = this;
            var chk_value_dept =[];
            var chk_value_zhuanjia =[];

            _this.deptids = '';
            _this.zhuanjiaids = '';

            $('input[name="dept"]:checked').each(function(){
                chk_value_dept.push($(this).val());
            });
            $('input[name="zhuanjia"]:checked').each(function(){
                chk_value_zhuanjia.push($(this).val());
            });
            if (chk_value_dept.length==0&&chk_value_zhuanjia.length==0){
                layer.alert('您还没有选择任何单位或专家！');
                return
            }else {
                for (var i = 0; i < chk_value_dept.length; i++) {
                    for (var j = 0; j < _this.deptlist.length; j++) {
                        if (chk_value_dept[i] == _this.deptlist[j].deptId) {
                            _this.deptids = _this.deptids + _this.deptlist[j].deptId + ",";
                        }
                    }
                }
                for (var i = 0; i < chk_value_zhuanjia.length; i++) {
                    for (var j = 0; j < _this.zhuanjialist.length; j++) {
                        if (chk_value_zhuanjia[i] == _this.zhuanjialist[j].zhuanjiaId) {
                            _this.zhuanjiaids = _this.zhuanjiaids + _this.zhuanjialist[j].zhuanjiaId + ",";
                        }
                    }
                }
            }
            $.post('/system/jingfeidept/fafang', {
                deptIdString: _this.deptids,
                zhuanjiaIdString: _this.zhuanjiaids,
                jingfeipiciId: _this.jingfeipiciid
            }, function (ppData) {
                if (ppData != null) {
                    if (ppData.result == "1") {
                        layer.open({
                            time: 1000,
                            btn: [],
                            content: "发放成功!",
                        })
                        _this.bindJingfeiPiciList()
                        $("#fafangModal").modal('hide');
                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");
        },

        //
        toJingfeiPiciDetail : function(jingfeipiciitem){
            $.modal.openTab("单位拨款详情",'system/jingfeipici/jingfei_dept?jingfeipiciid='+jingfeipiciitem.id+"&deptid="+jingfeipiciitem.deptid,false);
        },

        //全选按钮的点击事件
        checkAll1(){
            //获取id的dom元素
            let Id = document.getElementById('allAndNotAll1');
            //获取类名为chBox的dom元素
            let chBox = document.getElementsByClassName('dept');
            let len = chBox.length;
            //输出观察

            for(let i=0;i<len;i++){
                //当全选按钮为true时，全部按钮都为true，否则相反
                if(Id.checked){
                    chBox[i].checked=true;
                }
                else {
                    chBox[i].checked=false;
                }
            }
        },
        //单个按钮的点击事件
        checkOne1(){
            let count=0;
            let chBox = document.getElementsByClassName('dept');
            let len = chBox.length;
            for(let i=0;i<len;i++){
                //记录单选按钮为true的数量
                if(chBox[i].checked){
                    count++;
                }
            }
            //当单选按钮都为true时，全选按钮也为true，否则为false
            if(count==len){
                document.getElementById('allAndNotAll1').checked = true;
            }else {
                document.getElementById('allAndNotAll1').checked = false;
            }
        },

        //全选按钮的点击事件
        checkAll2(){
            //获取id的dom元素
            let Id = document.getElementById('allAndNotAll2');
            //获取类名为chBox的dom元素
            let chBox = document.getElementsByClassName('zhuanjia');
            let len = chBox.length;
            //输出观察

            for(let i=0;i<len;i++){
                //当全选按钮为true时，全部按钮都为true，否则相反
                if(Id.checked){
                    chBox[i].checked=true;
                }
                else {
                    chBox[i].checked=false;
                }
            }
        },
        //单个按钮的点击事件
        checkOne2(){
            let count=0;
            let chBox = document.getElementsByClassName('zhuanjia');
            let len = chBox.length;
            for(let i=0;i<len;i++){
                //记录单选按钮为true的数量
                if(chBox[i].checked){
                    count++;
                }
            }
            //当单选按钮都为true时，全选按钮也为true，否则为false
            if(count==len){
                document.getElementById('allAndNotAll2').checked = true;
            }else {
                document.getElementById('allAndNotAll2').checked = false;
            }
        },

    },
})